package com.mingqi.enetconnected.primary.mytask.activity;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.TextView;

import com.mingqi.enetconnected.R;
import com.mingqi.enetconnected.base.BaseActivity;
import com.mingqi.enetconnected.base.MyApplication;
import com.mingqi.enetconnected.util.AppUtils;
import com.mingqi.enetconnected.view.Dialog.single.SinglePickerHintManager;
import com.mingqi.enetconnected.view.Dialog.single.SinglePickerManager;
import com.mingqi.enetconnected.view.Dialog.single.SinglePickerTimeManager;
import com.mingqi.enetconnected.view.Dialog.single.SinglePickerTimerEmpManager;
import com.mingqi.enetconnected.view.Dialog.single.TimeValue;
import com.mingqi.enetconnected.view.HorizontalImageLayout;
import com.mingqi.enetconnected.view.HorizontalLayout;
import com.mingqi.enetconnected.view.HorizontalTextSwitchLayout;

import java.io.File;

import butterknife.BindView;

/**
 * 创建任务
 */
public class CreateTaskActivity extends BaseActivity {
    public static final int ACTIVITY_CAMERA = 1048;
    @BindView(R.id.image_layout)
    HorizontalImageLayout imageLayout;
    @BindView(R.id.business_types)
    HorizontalLayout businessTypes;
    @BindView(R.id.plan_time)
    HorizontalLayout planTime;
    @BindView(R.id.assgin_layout)
    HorizontalTextSwitchLayout assginLayout;
    @BindView(R.id.service_personnel)
    HorizontalLayout servicePersonnel;

    @BindView(R.id.service_content_layout)
    HorizontalLayout serviceContentLayout;
    @BindView(R.id.tv_urge)
    TextView urge;
    @BindView(R.id.tv_alarm)
    TextView alarm;


    @Override
    protected int getLayoutID() {
        return R.layout.activity_createtask;
    }

    @Override
    protected void initView() {
        setTitleName(R.string.create_task);
        setRightImage(R.drawable.ic_bar_message, new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(NotificationMessageActivity.class);
            }
        });

        urge.setVisibility(View.INVISIBLE);
        alarm.setVisibility(View.INVISIBLE);

        imageLayout.setTakePhoto(new HorizontalImageLayout.TakePhoto() {
            @Override
            public void takePhoto() {
                takePhtoto();
            }
        });
        serviceContentLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                SinglePickerHintManager singlePickerManager = new SinglePickerHintManager(CreateTaskActivity.this, -1);
                singlePickerManager.show();
            }
        });
        planTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (assginLayout.getSelectedTab() == 0) {
                    SinglePickerTimeManager singlePickerTimeManager = new SinglePickerTimeManager(CreateTaskActivity.this);
                    singlePickerTimeManager.setTitle(R.string.please_choose_plan_time);
                    singlePickerTimeManager.setOnConfirmListener(new SinglePickerTimeManager.onConfirmClickListener() {
                        @Override
                        public void onClick(TimeValue checkedItemValue) {
                            planTime.setRightText(checkedItemValue.getValue());
                        }
                    });
                    singlePickerTimeManager.show();
                } else {
                    SinglePickerTimerEmpManager singlePickerTimeManager = new SinglePickerTimerEmpManager(CreateTaskActivity.this);
                    singlePickerTimeManager.setTitle(R.string.please_choose_plan_time);
                    singlePickerTimeManager.setOnConfirmListener(new SinglePickerTimeManager.onConfirmClickListener() {
                        @Override
                        public void onClick(TimeValue checkedItemValue) {
                            planTime.setRightText(checkedItemValue.getValue());
                            servicePersonnel.setRightText(checkedItemValue.getEmpValue());
                        }
                    });
                    singlePickerTimeManager.show();
                }
            }
        });
    }

    private String name = "";

    private void takePhtoto() {
        Intent xx = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        name = MyApplication.PIC_PATH + "/" + AppUtils.getPhotoName();
        if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
            xx.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(new File(
                    name)));
        }
        startActivityForResult(xx, ACTIVITY_CAMERA);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == ACTIVITY_CAMERA) {
                //  writeLatestImage();
                imageLayout.refresh(name);
            }
        }


    }


}
